package ma.ensao.evalmetrics.interpretation;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ma.ensao.evalmetrics.controller.ClasseManager;
import ma.ensao.evalmetrics.controller.ClasseManager;
import ma.ensao.evalmetrics.controller.MethodManager;
import ma.ensao.evalmetrics.model.ClassInterp;
import ma.ensao.evalmetrics.model.MethodInterp;
import ma.ensao.evalmetrics.model.ResultClasse;
import ma.ensao.evalmetrics.model.ResultClasse;
import ma.ensao.evalmetrics.model.ResultMethod;

import com.opensymphony.xwork2.ActionSupport;

public class CLassIntManager extends ActionSupport{
	
	private Long idp;
	private ClasseManager classmanager;
	private List<ResultClasse> resultclass;
	private List<ClassInterp> records;
	private ClassInterp record;
	private String result;
	private int TotalRecordCount,jtStartIndex,jtPageSize;
	String jtSorting;
	
	//data from POST to create new class
	
	private Long id;
	private Long idProjet;
	private int idtest;
	private String ClasseName;
	private double wmc;
	private double ca;
	private double ce;
	private double instab;
	private double D;
	private int nom;
	private java.util.Date date = new java.util.Date();
	private double dit;
	
public String afficher() {
		
		records = new ArrayList<ClassInterp>() ;
		classmanager = new ClasseManager();
		System.out.println("start"+jtStartIndex+"size"+jtPageSize);
		this.resultclass=classmanager.listAll(idp,jtStartIndex,jtPageSize,jtSorting);
		ClassInterp myResult;
		for(ResultClasse res : resultclass){
			myResult = map(res);
			records.add(myResult);
		}
		result="OK";
		TotalRecordCount = classmanager.countTotal(idp);

		return SUCCESS;
	}

public String ajouter() {
	record = new ClassInterp() ;
	ResultClasse classe = new ResultClasse();

	classe.setDate(new Date());
	classe.setIdProjet(idp);
	classe.setClasseName(ClasseName);
	classe.setCa(ca);
	classe.setCe(ce);
	classe.setD(D);
	classe.setDit(dit);
	classe.setInstab(instab);
	classe.setNom(nom);
	classe.setWmc(wmc);

	classmanager = new ClasseManager();
	classe = classmanager.addClass(classe);
	
	result="OK";
	record = map(classe);
	
	return SUCCESS;
}

public String update() {
	record = new ClassInterp() ;
	ResultClasse classe = new ResultClasse();

	classe.setDate(new Date());
	classe.setIdProjet(idp);
	classe.setClasseName(ClasseName);
	classe.setCa(ca);
	classe.setCe(ce);
	classe.setD(D);
	classe.setDit(dit);
	classe.setInstab(instab);
	classe.setNom(nom);
	classe.setWmc(wmc);
	classe.setId(id);
	System.out.println("id test"+idtest+" wmc "+wmc);
	classe.setIdtest(idtest);

	classmanager = new ClasseManager();
	classe = classmanager.updateClass(classe);
	record = map(classe);
	
	result="OK";
	
	return SUCCESS;
}

	public String delete() {
	
	ResultClasse classe = new ResultClasse();

	classe.setId(id);
	
	classmanager = new ClasseManager();
	classmanager.deleteClass(classe);
	
	result="OK";
	
	return SUCCESS;
}

private ClassInterp map(ResultClasse result){
	ClassInterp myResult = new ClassInterp();
	
	myResult.setDate(result.getDate());
	myResult.setId(result.getId());
	myResult.setIdProjet(result.getIdProjet());
	myResult.setClasseName(result.getClasseName());
	myResult.setIdtest(result.getIdtest());
	myResult.setCa(result.getCa());
	myResult.setCe(result.getCe());
	myResult.setD(result.getD());
	myResult.setDit(result.getDit());
	myResult.setInstab(result.getInstab());
	myResult.setNom(result.getNom());
	myResult.setWmc(result.getWmc());
	
	double nomInt;
	if(result.getNom()<=20){
		nomInt = 3;
	}
	else if( 20 < result.getNom() && result.getNom()<=30){
		nomInt = 2;
	}
	else if(30<result.getNom() && result.getNom()<=60){
		nomInt =1;
	}
	else {
		nomInt = 0;
	}
	nomInt=new BigDecimal(nomInt ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
	myResult.setNomInt(nomInt);
	
	double ditInt;
	if(result.getDit()<=5){
		ditInt=3;
	}
	else if(5<result.getDit() && result.getDit()<=6){
		ditInt = 2;
	}
	else if(6<result.getDit() && result.getDit()<=7){
		ditInt = 1;
	}
	else{
		ditInt = 0;
	}
	ditInt = new BigDecimal(ditInt ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
	myResult.setDitInt(ditInt);
	
	double wmcInt;
	if(result.getWmc()>=80){
		wmcInt=Math.pow(2,(30-result.getNom())/10);
	}
	else if(50<=result.getWmc() && result.getWmc()<80){
		wmcInt=Math.pow(2,(20-result.getNom())/30);
	}
	else if(30<=result.getWmc() && result.getWmc()<50){
		wmcInt=Math.pow(3,(15-result.getNom())/15);
	}
	else{
		wmcInt = 3;
	}
	wmcInt = new BigDecimal(wmcInt ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
	myResult.setWmcInt(wmcInt);
	
	double instabInt;
	instabInt = 3-(2*((25-result.getInstab())/25));
	instabInt = new BigDecimal(instabInt ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
	myResult.setInstabInt(instabInt);
	
	double ceInt;
	if(result.getCe()<=6){
		ceInt=3;
	}
	else{
		ceInt=Math.pow(2,(10-result.getCe())/2);
	}
	ceInt = new BigDecimal(ceInt ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
	myResult.setCeInt(ceInt);
	
	double caInt;
	if(result.getCa()<=19){
		caInt=3;
	}
	else{
		caInt=Math.pow(2,(30-result.getCa())/7);
	}
	caInt = new BigDecimal(caInt ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
	myResult.setCaInt(caInt);
	
	return myResult;
}

public Long getIdp() {
	return idp;
}

public void setIdp(Long idp) {
	this.idp = idp;
}

public List<ClassInterp> getRecords() {
	return records;
}

public void setRecords(List<ClassInterp> records) {
	this.records = records;
}

public ClassInterp getRecord() {
	return record;
}

public void setRecord(ClassInterp record) {
	this.record = record;
}

public String getResult() {
	return result;
}

public void setResult(String result) {
	this.result = result;
}

public int getTotalRecordCount() {
	return TotalRecordCount;
}

public void setTotalRecordCount(int totalRecordCount) {
	TotalRecordCount = totalRecordCount;
}

public int getJtStartIndex() {
	return jtStartIndex;
}

public void setJtStartIndex(int jtStartIndex) {
	this.jtStartIndex = jtStartIndex;
}

public int getJtPageSize() {
	return jtPageSize;
}

public void setJtPageSize(int jtPageSize) {
	this.jtPageSize = jtPageSize;
}

public String getJtSorting() {
	return jtSorting;
}

public void setJtSorting(String jtSorting) {
	this.jtSorting = jtSorting;
}

public Long getId() {
	return id;
}

public void setId(Long id) {
	this.id = id;
}

public Long getIdProjet() {
	return idProjet;
}

public void setIdProjet(Long idProjet) {
	this.idProjet = idProjet;
}

public int getIdtest() {
	return idtest;
}

public void setIdtest(int idtest) {
	this.idtest = idtest;
}

public String getClasseName() {
	return ClasseName;
}

public void setClasseName(String classeName) {
	ClasseName = classeName;
}

public double getWmc() {
	return wmc;
}

public void setWmc(double wmc) {
	this.wmc = wmc;
}

public double getCa() {
	return ca;
}

public void setCa(double ca) {
	this.ca = ca;
}

public double getCe() {
	return ce;
}

public void setCe(double ce) {
	this.ce = ce;
}

public double getInstab() {
	return instab;
}

public void setInstab(double instab) {
	this.instab = instab;
}

public double getD() {
	return D;
}

public void setD(double d) {
	D = d;
}

public int getNom() {
	return nom;
}

public void setNom(int nom) {
	this.nom = nom;
}

public java.util.Date getDate() {
	return date;
}

public void setDate(java.util.Date date) {
	this.date = date;
}

public double getDit() {
	return dit;
}

public void setDit(double dit) {
	this.dit = dit;
}
	
	
}
